﻿using System;
using Ext.Net;

namespace Sharp.Portal.ExtNet.Extend
{
    /// <summary>
    /// 外键选取框
    /// </summary>
    public class ForeignSelectBox : Container
    {
        /// <summary>
        /// 外键选取框
        /// </summary>
        public ForeignSelectBox()
        {
            this.Layout = "hbox";

        }
        /// <summary>
        /// WindowUrl
        /// </summary>
        public string WindowUrl { get; set; }
        /// <summary>
        /// 字段标签
        /// </summary>
        public string FieldLabel { get; set; }
        /// <summary>
        /// OnLoad 事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Items.Add(new Ext.Net.TextField()
            {
                Text = this.Text,
                ReadOnly = true,
                FieldLabel = this.FieldLabel
            });
            var openButton = new Ext.Net.Button()
            {
                Text = "...",
                DirectClickUrl = this.WindowUrl
            }.ToBuilder();
            openButton.DirectEvents(x =>
            {
                x.Click.ExtraParams.Add(new Parameter("controlId", "this.up('container').getId()",
                    ParameterMode.Raw));
            });
            this.Items.Add(openButton);
            this.Items.Add(new Hidden()
            {
                Name = this.Name,
                Value = this.Value
            });
        }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 文本
        /// </summary>
        public string Text { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public string Value { get; set; }
        /// <summary>
        /// 转为Builder
        /// </summary>
        /// <returns></returns>
        public new Builder ToBuilder()
        {
            return new Builder(this);
        }
        /// <summary>
        /// Builder
        /// </summary>
        public new class Builder : Builder<ForeignSelectBox, Builder>
        {
            private ForeignSelectBox _component;

            /// <summary>
            /// Builder
            /// </summary>
            /// <param name="component"></param>
            public Builder(ForeignSelectBox component) : base(component)
            {
                _component = component;
            }
            /// <summary>
            /// 名称
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public Builder Name(string name)
            {
                _component.Name = name;
                return _component.ToBuilder();
            }
            /// <summary>
            /// WindowUrl
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public Builder WindowUrl(string url)
            {
                _component.WindowUrl = url;
                return _component.ToBuilder();
            }
            /// <summary>
            /// 字段标签
            /// </summary>
            /// <param name="fieldLabel"></param>
            /// <returns></returns>
            public Builder FieldLabel(string fieldLabel)
            {
                _component.FieldLabel = fieldLabel;
                return _component.ToBuilder();
            }
            /// <summary>
            /// 文本
            /// </summary>
            /// <param name="text"></param>
            /// <returns></returns>
            public Builder Text(string text)
            {
                _component.Text = text;
                return _component.ToBuilder();
            }
            /// <summary>
            /// 值
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public Builder Value(object value)
            {
                _component.Value = Convert.ToString(value);
                return _component.ToBuilder();
            }
        }
    }
}